var icons = require('./icon.js');
// var CryptoJS = require('./CryptoJS.js');
// var key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为秘钥
// var iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); //十六位十六进制数作为秘钥偏移量
// var iv = CryptoJS.enc.Utf8.parse('1');; //十六位十六进制数作为秘钥偏移量

const padLeftZero = str => {
  return ('00' + str).substr(str.length);
}

//获取一个字符串的最后四位数字
const getLastFourNumber = str => {
  return str.substr(str.length - 4)
}
// 加密算法
const encryptAES = word => {
  var srcs = CryptoJS.enc.Utf8.parse(word);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  console.log(encrypted);
  return encrypted.ciphertext.toString().toUpperCase();
}
// 解密算法
const decryptAES = word => {
  var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}


/**
 * 获取图标class
 */
const getClassName = id => {
  let className = '';
  for (let index = 0; index < icons.length; index++) {
    if (icons[index].id == id) {
      className = icons[index].className;
      return className;
    }
  }
  if (!className) {
    return 'icon-fenleiorguangchangorqita';
  }
}
//去掉所有空格
const trimAll = str => {
  return str.replace(/\s+/g, '');
}
//检测银行卡号是否正确
const isBankcard = str => {
  let pattern = /^([1-9]{1})(\d{14}|\d{20})$/;
  if (trimAll(str).length > 13 && trimAll(str).length < 20) {
    return true;
  } else {
    return false;
  }
}
//比较两个时间的大小
const compareTime = (startTime, endTime) => {
  let startDate = new Date(startTime).getTime();
  let endDate = new Date(endTime).getTime();
  let nowDate = new Date().getTime();
  if (startDate < endDate && endDate < nowDate) {
    return true;
  } else {
    return false;
  }
}
//形成一定格式的时间
const formatTime = (date, fmt) => {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}
//获取一点范围内的时间
const getBetweenTime = (date, recentDays) => {
  var startTime = date;
  if (!date) {
    startTime = new Date();
  }
  if (isNaN(recentDays)) {
    return formatTime(date, 'yyyy-MM-dd');
  } else {
    let day = startTime.getDate() + parseInt(recentDays);
    startTime.setDate(day);
    return formatTime(startTime, 'yyyy-MM-dd')
  }
}


module.exports = {
  encryptAES: encryptAES,
  decryptAES: decryptAES,
  formatTime: formatTime,
  getBetweenTime: getBetweenTime,
  trimAll: trimAll,
  isBankcard: isBankcard,
  compareTime: compareTime,
  getClassName: getClassName,
  getLastFourNumber: getLastFourNumber
}